package com.mjlf.mjatis.mapper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mjlf.mjatis.alias.Alias;
import com.mjlf.mjatis.alias.AliasUtils;
import com.mjlf.mjatis.config.Configuration;
import com.mjlf.mjatis.exception.MapperException;
import com.mjlf.mjatis.result.ResultMap;
import com.mjlf.mjatis.result.ResultMapUtil;
import lombok.extern.java.Log;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName MapperUtil
 * @Author mjlft
 * @Date 2020/5/29 23:07
 * @Version 1.0
 * @Description mapper工具类
 */
@Log
public final class MapperUtil {

    public static void parse(JSONArray mapperArray, Configuration configuration){
        if(mapperArray == null){
            return;
        }
        for(int i = 0; i < mapperArray.size(); i ++){
            JSONObject jsonObject = mapperArray.getJSONObject(i);
            MapperStatement mapperStatement = parse(jsonObject, configuration);
            if(mapperStatement != null){
                configuration.getMapperStatements().put(mapperStatement.getNamespace(), mapperStatement);
            }
        }
    }

    /**
     * 解析Mapper Json文件
     * @param jsonObject
     * @return
     */
    public static MapperStatement parse(JSONObject jsonObject, Configuration configuration){
        if(jsonObject != null){
            MapperStatement mapperStatement = new MapperStatement();

            //将Mapper Json 配置转换为 MapperStatement文件
            //需要主要的是， 这里只是将json转换成了MapperStatement对象， 后续还有很多工作需要做
            //？？？？
            mapperStatement.setNamespace(jsonObject.getString("namespace"));

            //解析ResultMap
            try {
                Set<ResultMap> resultMaps = ResultMapUtil.parse(jsonObject.getJSONArray("resultMaps"));
                if(configuration.getResultMaps() == null){
                    configuration.setResultMaps(resultMaps);
                }else {
                    configuration.getResultMaps().addAll(resultMaps);
                }
            }catch (Exception e){
                log.info("解析ResultMapper失败");
            }
            //解析别名
            try{
                Set<Alias> aliases = AliasUtils.parse(jsonObject.getJSONArray("alias"));
                if(configuration.getAliases() == null){
                    configuration.setAliases(aliases);
                }else {
                    configuration.getAliases().addAll(aliases);
                }
            }catch (Exception e){
                log.info("解析Alias失败");
            }
            //解析Sql
            try {
                Set<MapperMethod> mapperMethods = MapperMethodUtil.parse(jsonObject.getJSONArray("mql"));
                mapperMethods.stream().forEach(mapperMethod -> {
                    MapperMethodUtil.parseResultType(mapperMethod, configuration);
                });
                if(mapperStatement.getMapperMethods() == null){
                    mapperStatement.setMapperMethods(mapperMethods);
                }else {
                    mapperStatement.getMapperMethods().addAll(mapperMethods);
                }
                return mapperStatement;
            } catch (Exception e){
                log.info("解析Mapper失败");
                MapperException mapperException =  new MapperException("解析Mapper失败");
                mapperException.setStackTrace(e.getStackTrace());
                throw mapperException;
            }
        }
        return null;
    }
}
